﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 数据库复制工具toSQLServer
{
    public partial class Form_ChangeColumns : Form_Parent
    {
        TableMessage[] array;
        public Form_ChangeColumns()
        {
            InitializeComponent();
            foreach (TableMessage tms in Messages)
            {
                this.comboBox1.Items.Add(tms.TableName);
            }
            this.comboBox1.SelectedIndex = 0;
            array=new TableMessage[Messages.Count];
            Messages.CopyTo(array);
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            checkedListBox1.Items.AddRange(db.GetColumnList(comboBox1.Text.Trim(), srcType,"").ToArray());
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox2.Text= checkedListBox1.SelectedItem.ToString()+"=";
            foreach (TableMessage tbm in array)
            {
                if (tbm.TableName == comboBox1.Text.Trim())
                {
                    tbm.Colums.Clear();
                    for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
                    {
                        tbm.Colums.Add(checkedListBox1.CheckedItems[i].ToString());
                    }
                    richTextBox1.Text = tbm.GetSqlCmd();
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (TableMessage tbm in array)
            {
                if (tbm.TableName == comboBox1.Text.Trim())
                {
                    //tbm.Fileter.Clear();
                    tbm.Fileter.Add(textBox2.Text.Trim());
                    richTextBox1.Text = tbm.GetSqlCmd();
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            foreach (TableMessage tbm in array)
            {
                if (tbm.TableName == comboBox1.Text.Trim())
                {
                    tbm.Clear();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否保存设置？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Messages.Clear();
                foreach (TableMessage tm in array)
                {
                    Messages.Add(tm);
                }
                this.Close();
            }
            else { this.Close(); }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
